﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using SevenShine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.SceneManagement;

public class Level5BeginEnd : MonoBehaviour {

    private GameObject dialogPanel;
    private AudioSource audios;

    //public AudioClip TypeWritingClip;
    public AudioClip RunClip;
    public AudioClip Pass;
    public AudioClip DialogClip;

    // Use this for initialization
    void Start()
    {
        audios = GameObject.Find("Main Camera").GetComponent<AudioSource>();
        Prepare();
    }

    void Prepare()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(TextShow(() =>
        {
            StartCoroutine(Run(() =>
            {
                //StartCoroutine(Dialog(() =>
                //{
                    StartCoroutine(GameBegin(() =>
                    {
                        Debug.Log("Animation Finish!");
                    }));

                //}));
            }));
        }));
    }

    IEnumerator TextShow(Action action = null)
    {
        yield return new WaitForSeconds(0);
        Text text = GameObject.Find("Canvas/Text").GetComponent<Text>();

        //audios.clip = TypeWritingClip;
        //audios.Play();

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level5_Panbai"), 2).SetEase(Ease.Linear).OnComplete(() =>
        // {
        //     audios.Stop();

        //     text.gameObject.SetActive(false);
        //     if (action != null)
        //     {
        //         action();
        //     }
        // });

        text.text = LanguageMgr.GetInstance().ShowText("Level5_Panbai");
        yield return new WaitForSeconds(2);
        text.gameObject.SetActive(false);
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Run(Action action = null)
    {
        yield return new WaitForSeconds(0);
        Parkour.Instance.ani.SetTrigger("Run");

        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        Parkour.Instance.XiaoQi.transform.DOMoveZ(0, 1).SetEase(Ease.Linear).OnComplete(() =>
         {
             audios.Stop();
             Parkour.Instance.ani.SetTrigger("Idle");

             if (action != null)
             {
                 action();
             }
         });
    }

    //IEnumerator Dialog(Action action = null)
    //{
    //    yield return new WaitForSeconds(0);
    //    Instantiate(Resources.Load("Prefabs/UI/DialogImage"), transform);
    //    dialogPanel = GameObject.Find("Canvas/DialogImage(Clone)");
    //    Text dialogText = GameObject.Find("Canvas/DialogImage(Clone)/Text").GetComponent<Text>();
    //    dialogText.DOText(LanguageMgr.GetInstance().ShowText("Level5_Dialog1"), 2).SetEase(Ease.Linear).OnComplete(() =>
    //     {
    //         dialogPanel.SetActive(false);
    //         dialogText.text = "";


    //         if (action != null)
    //         {
    //             action();
    //         }
    //     });
    //}

    IEnumerator GameBegin(Action action = null)
    {
        yield return new WaitForSeconds(0);
        Parkour.Instance.canTouch = true;
        if (action != null)
        {
            action();
        }
    }

    void EndDialog()
    {
        Instantiate(Resources.Load("Prefabs/UI/DialogImage"), transform);
        dialogPanel = GameObject.Find("Canvas/DialogImage(Clone)");
        Text dialogText = GameObject.Find("Canvas/DialogImage(Clone)/Text").GetComponent<Text>();

        audios.clip = DialogClip;
        audios.Play();
        audios.loop = false;
        float length = DialogClip.length;
        
        dialogText.DOText(LanguageMgr.GetInstance().ShowText("Level5_Dialog"), length).SetEase(Ease.Linear).OnComplete(() =>
        {
            audios.clip = Pass;
            audios.Play();
            audios.loop = false;
            StartCoroutine("JumpNextScene");
        });
    }

    IEnumerator JumpNextScene()
    {
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene("006");
    }
}
